<?php

namespace IDock\Insurance\DBIC\Response;

use IDock\Http\Response;
use IDock\Printer\ProposalStatusPrinter;

class ProposalStatusResponse extends AllDBICResponse
{

    /**
     * @var ProposalStatusPrinter $printer
     */
    public $printer = ProposalStatusPrinter::class;

    public $list = [];

    public function printerInit(): Response
    {
        $list = $this->getRecords();
        $this->list = $list ?: [];
        $status1 = $status2 = '1';

        foreach ($list as $key => $value) {
            if ($key) {
                $status1 = $value['status'] ?? '1';
            } else {
                $status2 = $value['status'] ?? '1';
            }
        }

        //核保通过
        if (in_array($status1, ['1', '3', '4']) and in_array($status2, ['1', '3', '4'])) {
            $this->printer->statusCode = 'U1';
        }

        //核保退回
        if (in_array($status1, ['2']) or in_array($status2, ['2'])) {
            $this->printer->statusCode = 'U2';
        }

        //人工核保
        if ($status1 == '9' or $status2 == '9') {
            $this->printer->statusCode = 'U3';
        }

        //核保失败
        if (in_array($status1, ['5', '6']) or in_array($status2, ['5', '6'])) {
            $this->printer->statusCode = 'U4';
        }

        //已出保单
        if ($status1 == '10' or $status2 == '10') {
            $this->printer->statusCode = 'U5';
        }

        return $this;
    }

    public function setCiInquiryNo(string $inquiryNo)
    {
        $this->printer->ciInquiryNo = $inquiryNo;

        foreach ($this->getRecords() as $value) {
            if ($this->printer->ciInquiryNo == ($value['proposalNo'] ?? '')) {
                $this->printer->ciPolicyNo = $value['policyNo'] ?? '';
            }
        }
    }

    public function setBiInquiryNo(string $inquiryNo)
    {
        $this->printer->biInquiryNo = $inquiryNo;

        foreach ($this->getRecords() as $value) {
            if ($this->printer->biInquiryNo == ($value['proposalNo'] ?? '')) {
                $this->printer->biPolicyNo = $value['policyNo'] ?? '';
            }

        }
    }
}
